<!DOCTYPE html> 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Registration</title>

	<!-- start Scott's code -->
	<!-- Latest compiled and minified CSS -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
	<!-- end Scott's code -->

    </head>
    <body>
<?php
/* Start Ben's code */
 if(isset($_POST['submit_btn']))
 {
  // Store the passed values into there own variables. (Easier to work with) 
  $first = $_POST['first'];
  $last = $_POST['last'];
  $username = $_POST['email'];
  $reason = $_POST['reason'];
  $password = $_POST['password'];
  $encrpytPass = md5($password);
  // Store the name of the XML file you want to store the information into.
  //  -- So you can just change this variable if you want to store info in diffrent file.
  $file = 'applicant.xml';

  // Create a new DOM document
  $dom = new DOMDocument();
  
  // Load the XML file into the DOM document that was just created. 
  $dom->load($file) or die("Error loading XML file"); // rename xml file
  
   // This just makes the output format correctly, not explicitly used 
  // -- but nice to have if you need to output any of the information. 
  $dom->formatOutput = true;
  
  // Get the document element, the root tag in the XML file. 
  // -- Which is currently <info> for the XML file being used.  
  $root = $dom->documentElement;

  /* Adding a new email */
  // Creates the <Email> tag.
  $newEmail = $dom->createElement("Email");
  // Adds values between the tags <Email>$username</Email>. 
  $emailText = $dom->createTextNode($username);
  // Appends the text to the <Email> tag. 
  $newEmail->appendChild($emailText);
  
  /* Adding a new first name */
  // Create the <First> tag.
  $newFirst = $dom->createElement('First');
  // Adds the actual first name between the tags <First>$first</First>.  
  $firstText = $dom->createTextNode($first);
  // Appends the text to the <First> tag. 
  $newFirst->appendChild($firstText);
  
  // Create the <Last> tag
  $newLast = $dom->createElement('Last');
  // Adds the actual first name between the tags <Last>$last</Last>.
  $lastText = $dom->createTextNode($last);
  // Appends the text to the <Last> tag. 
  $newLast->appendChild($lastText);
  
  // Create the <Password> tag
  $newPass = $dom->createElement('Password');
  // Adds the actual first name between the tags <Password>$pass</Password>.
  /* changed $dom->createTextNode($password) to the following*/
  $passText = $dom->createTextNode($encrpytPass);
  // Appends the text to the <Password> tag. 
  $newPass->appendChild($passText);
  
  // Create the <Reason> tag
  $newReason = $dom->createElement('Reason');
  // Adds the actual first name between the tags <Reason>$reason</Reason>.
  $reasonText = $dom->createTextNode($reason);
  // Appends the text to the <Reason> tag.
  $newReason->appendChild($reasonText);
 
  $newPermisson = $dom->createElement('Permission');
  $permissionText = $dom->createTextNode('0');
  $newPermisson->appendChild($permissionText);
// Appends all the previos to user so that it can be stored. 
  $user = $dom->createElement("user");
  $user->appendChild($newEmail);
  $user->appendChild($newFirst);
  $user->appendChild($newLast);
  $user->appendChild($newPass);
  $user->appendChild($newReason);
  $user->appendChild($newPermisson);
// now appeneds user to root which is <info>
  $root->appendChild($user);

// save changes to the DOM file to the original.
  $dom->save($file);
  
  
// Send email to Dr. Finkel   
  $to = "raphael@cs.uky.edu";
  $subject = "Sholem OCR: New Applicant";
  $message = "A new applicant is awaiting your approval. \n Name: " . $first." " . $last .". \n Email: " . $username . "\n Reason: ". $reason;
  mail($to,$subject,$message); 
  
  
// send a success message.
  echo '<div class="col-md-12 row bg-primary text-center"><h1>All Works of Sholem Aleichem</h1></div>';
  echo '    <div class="container">';
  echo '        <div class="text-center container">';
  echo '            <h2>Thank You</h2>';
  echo '            <h4>Your request has been sent. Dr. Finkel will contact you as soon as possible.</h4>';
  echo '            <h4><a href="searchSholem.cgi"><span class="glyphicon glyphicon-menu-left" aria-hidden="true"></span> Home</a></h4>';
  echo '        </div>';
  echo '    </div>';
  }

// used to control what is displayed to the screen. 
  else
  {
/* End Ben's code */
?>
<!-- start Scott's code -->
<div class="col-md-12 row bg-primary text-center">
    <h1>All Works of Sholem Aleichem - Registration</h1>
</div>
<div class="container">

    <div class="text-center container">
        <h3>Please enter your information and a (short) reason why you want to work on this project.</h3>
    </div>

    <!-- form to collect all the applicants information -->
    <form action="registration.php" method="POST" class="form-horizontal">
        <div class="form-group">
            <!-- First name -->
            <label for="inputFirst" class="col-sm-2 control-label">First</label>
            <div class="col-sm-5 col-md-3">
                <input type="text" class="form-control" id="inputFirst" name="first" placeholder="First name">
            </div>
        </div>
        <div class="form-group">
	    <!-- Last name -->
            <label for="inputLast" class="col-sm-2 control-label">Last</label>
            <div class="col-sm-5 col-md-3">
                <input type="text" class="form-control" id="inputLast" name="last" placeholder="Last name">
	    </div>
        </div>
        <div class="form-group">
	    <!-- Email -->
	    <label for="inputEmail" class="col-sm-2 control-label">Email</label>
	    <div class="col-sm-5 col-md-3">
	        <input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email">
	    </div>
        </div>
        <div class="form-group">
	    <!-- Password -->
	    <label for="inputPassword" class="col-sm-2 control-label">Password</label>
	    <div class="col-sm-5 col-md-3">
	        <input type="password" class="form-control" id="inputPassword" name="password" placeholder="Password">
	    </div>
        </div>
        <div class="form-group">
	    <!-- Reason for wanting to edit the works of Sholem -->
	    <label for="inputReason" class="col-sm-2 control-label">Reason</label>
	    <div class="col-sm-5 col-md-3">
	        <textarea name="reason" cols="50" rows="5" placeholder="Enter reason here..."></textarea>
	    </div>
        </div>
        <div class="form-group">
	    <div class="col-sm-2"></div>
	    <!-- Submit button -->
	    <input class="btn btn-primary" type="submit" name="submit_btn" id="submit" value="Submit">
        </div>
    <!-- end form -->
    </form>
</div>
<!-- end Scott's code -->

<?php
}
?>
    <!-- start Scott's code -->
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <!-- start Scott's code -->

    </body>
</html>
